<?php
namespace App\Services;

use \App\Helpers\CURL;

/**
 * In-Apple Purchase 服务
 * @package App\Services
 */
class InApplePurchaseService {

    /**
     * In-Apple Purchase配置信息
     * @var \Phalcon\Config
     */
    private $config;

    /**
     * 验证收据返回的数据
     * @var array
     */
    private $message = array(
        "21000" => "The App Store could not read the JSON object you provided.",
        "21002" => "The data in the receipt-data property was malformed.",
        "21003" => "The receipt could not be authenticated.",
        "21004" => "The shared secret you provided does not match the shared secret on file for your account.Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.",
        "21005" => "The receipt server is not currently available.",
        "21006" => "This receipt is valid but the subscription has expired. When this status code is returned to your server, the receipt data is also decoded and returned as part of the response.Only returned for iOS 6 style transaction receipts for auto-renewable subscriptions.",
        "21007" => "This receipt is a sandbox receipt, but it was sent to the production server.",
        "21008" => "This receipt is a production receipt, but it was sent to the sandbox server."
    );

    function __construct($config) {
        $this->config = $config;
    }

    #region 验证内购收据信息

    /**
     * 验证内购收据信息
     * @param $receiptData 内购收据信息
     * @return array
     */
    function verifyReceipt($receiptData) {
        // 认证收据 URL
        $url = $this->config->verifyReceiptUrl;
        // 请求数据
        $body["receipt-data"] = $receiptData;

        // 请求头信息
        $header = ['Content-Type:application/json'];
        // curl对象
        $curl = new CURL();
        // 发起curl请求
        $result = $curl->request($url, $body, $header);
        return $result;
    }

    #endregion


}